package com.ruoyi.whzj.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.whzj.domain.*;
import com.ruoyi.whzj.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


/**
 * 任务控制器
 */
@Controller
@RequestMapping(value = "/phone/whzj/activity")
public class WhActivityPhoneController extends BaseController {

    @Autowired
    private IWhUserService whUserService;

    @Autowired
    private IWhActivityService whActivityService;

    @Autowired
    private IWhActivityUserService whActivityUserService;

    @Autowired
    private IWhIntegralHistoryService whIntegralHistoryService;

    @Autowired
    private IWhActivityotherService whActivityotherService;

    @Autowired
    private IWhzjBaseconfigService whzjBaseconfigService;


    /**
     * 获取最新的一条活动信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/getActivityNew")
    @ResponseBody
    public AjaxResult getActivityNew(String userId)throws Exception
    {
        WhActivity activity = whActivityService.selectActivityNew();
        if(activity == null){
            return error("暂无活动");
        }
        WhzjBaseconfig baseconfig = whzjBaseconfigService.selectWhzjBaseconfigById("1");
        Integer activitynum = baseconfig.getActivitynum();
        WhActivityUser activityUser = new WhActivityUser();
        activityUser.setActivityId(activity.getId());
        activityUser.setUserId(userId);
        List<WhActivityUser> users = whActivityUserService.selectWhActivityUserList(activityUser);
        if(users.size() == 1){
            activitynum = activitynum - users.get(0).getNum();
        }
        activity.setActivitynum(activitynum);

        return success("操作成功", activity);
    }

    /**
     * 活动报名
     * @param userId
     * @param activityId
     * @param num
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveActivityUser")
    @ResponseBody
    public synchronized AjaxResult saveActivityUser(String userId, String activityId, int num)throws Exception
    {
        WhActivity activity = whActivityService.selectWhActivityById(activityId);
        int people = activity.getPeople();
        int allPeople = activity.getAllPeople();
        if(people == allPeople){
            return error("报名人数已满，活动截至。");
        }
        if(people + num > allPeople){
            return error("参与人员不能大于总次数");
        }
        int integral = activity.getIntegral();
        int allIntegral = integral * num;
        WhUser user = whUserService.selectWhUserById(userId);
        if(user.getIntegral() < allIntegral){
            return error("用户积分不足", user.getIntegral());
        }

        if("0".equals(user.getIsDisable())){
            return error("用户被封禁，请联系客服");
        }
        //修改活动数量
        WhActivity activity1 = new WhActivity();
        activity1.setId(activityId);
        activity1.setPeople(people + num);
        whActivityService.updateWhActivity(activity1);

        WhActivityUser activityUser = new WhActivityUser();
        activityUser.setActivityId(activityId);
        activityUser.setUserId(userId);
        //添加活动报名记录
        List<WhActivityUser> users = whActivityUserService.selectWhActivityUserList(activityUser);
        if(users.size() == 1){
            activityUser.setId(users.get(0).getId());
            activityUser.setNum(users.get(0).getNum() + num);
            activityUser.setIntegral(users.get(0).getIntegral() + allIntegral);
            whActivityUserService.updateWhActivityUser(activityUser);
        }else{
            activityUser.setId(IdWorker.getId());
            activityUser.setCreateTime(new Date());
            activityUser.setNum(num);
            activityUser.setIntegral(allIntegral);
            activityUser.setStatus("等待开奖");
            whActivityUserService.insertWhActivityUser(activityUser);
        }

        //报名人数满了添加一条新的活动记录
        if(people + num == allPeople){
            WhActivity whActivity = new WhActivity();
            whActivity.setId(IdWorker.getId());
            whActivity.setCreateTime(new Date());
            whActivity.setNum(activity.getNum() + 1);
            whActivity.setStatus("待开奖");
            whActivityService.insertWhActivity(whActivity);
        }

        //减少用户积分
        WhUser user1 = new WhUser();
        user1.setId(userId);
        user1.setIntegral(user.getIntegral() - allIntegral);
        whUserService.updateWhUser(user1);

        //积分明细
        WhIntegralHistory history = new WhIntegralHistory();
        history.setId(IdWorker.getId());
        history.setCreateTime(new Date());
        history.setUserId(userId);
        history.setIntegral(-allIntegral);
        history.setEventId(activityId);
        history.setOperat("9");
        whIntegralHistoryService.insertWhIntegralHistory(history);

        return success("报名成功");
    }


    /**
     * 获取我的报名记录
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getMyActivity")
    @ResponseBody
    public AjaxResult getMyActivity(String userId)throws Exception
    {
        WhActivityUser activityUser = new WhActivityUser();
        activityUser.setUserId(userId);
        List<WhActivityUser> users = whActivityUserService.selectWhActivityUserList(activityUser);
        if(users.size() == 0){
            return error("暂无报名记录");
        }
        return success("操作成功", users);
    }


    /**
     * 获取本次活动的参与人
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/getActivity")
    @ResponseBody
    public AjaxResult getActivity(String id)throws Exception
    {
        WhActivityUser activityUser = new WhActivityUser();
        activityUser.setActivityId(id);
        List<WhActivityUser> users = whActivityUserService.selectWhActivityUserList(activityUser);
        if(users.size() == 0){
            return error("暂无报名记录");
        }
        return success("操作成功", users);
    }

    /**
     * 获取其他活动列表
     * @return
     * @throws Exception
     */
    @RequestMapping("/getActivityOther")
    @ResponseBody
    public AjaxResult getActivityOther()throws Exception
    {
        List<WhActivityother> list = whActivityotherService.selectWhActivityotherList(new WhActivityother());
        return success("操作成功", list);
    }
}

